package com.wngbms.multidatasource;

import com.alibaba.fastjson.JSON;
import com.other.SimpleBean;
import com.wngbms.multidatasource.config.TConfig;
import com.wngbms.multidatasource.init.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@MapperScan("com.wngbms.multidatasource.mapper")
@Slf4j
@SpringBootApplication
public class MultiDatasourceApplication {

    public static void main(String[] args) {
        SpringApplication.run(MultiDatasourceApplication.class, args);

//        TConfig bean = SpringUtil.getBean(TConfig.class);
//        log.info(bean + "");
//        log.info(JSON.toJSONString(bean));

        log.info("启动成功");

        log.info("stack {}", Thread.currentThread().getStackTrace());

        RedissonClient redissonClient = SpringUtil.getBean(RedissonClient.class);
//        extracted(redissonClient);

        RBlockingQueue<String> blockingQueue = redissonClient.getBlockingQueue("bqdemo");
        RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(blockingQueue);

        new Thread(() -> {
            try {
                while (true) {
//                    Thread.sleep(20000);
                    String take = blockingQueue.take();
                    log.info("take:[{}]", take);
                }
            } catch (InterruptedException e) {
                log.error("{}", e.getMessage(), e);
            }
        }).start();

        new Thread(() -> {
            try {
                while (true) {
                    String uuid = UUID.randomUUID().toString();
                    log.info("offer:[{}]", uuid);
                    delayedQueue.offer(uuid, 30, TimeUnit.SECONDS);
                    Thread.sleep(60000);
                }
            } catch (InterruptedException e) {
                log.error("{}", e.getMessage(), e);
            }
        }).start();

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            log.info("=====shutdown hook");
        }));

        new SimpleBean();
    }

    private static void extracted(RedissonClient redissonClient) {
        RBlockingQueue<String> blockingQueue = redissonClient.getBlockingQueue("bqdemo");

        new Thread(() -> {
            try {
                while (true) {
                    Thread.sleep(4000);
                    String take = blockingQueue.take();
                    log.info("take:[{}]", take);
                }
            } catch (InterruptedException e) {
                log.error("{}", e.getMessage(), e);
            }
        }).start();

        new Thread(() -> {
            try {
                while (true) {
                    String uuid = UUID.randomUUID().toString();
                    log.info("offer:[{}]", uuid);
                    blockingQueue.offer(uuid, 5, TimeUnit.SECONDS);
                    Thread.sleep(3000);
                }
            } catch (InterruptedException e) {
                log.error("{}", e.getMessage(), e);
            }
        }).start();
    }

}
